package com.mingyizhudao.app.view;

import com.dixiang.framework.utils.Utils;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.Constants;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.doctor.DoctorDetailsActivity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

public class WebViewWithBackActivity extends MYBaseActivity{
	private View actionbar;
    private WebView advertise_webview;
    private ProgressBar progressBar;
    private String title;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_webview_withback);
	    
	    actionbar = findViewById(R.id.actionbar);
	    advertise_webview = (WebView) findViewById(R.id.advertise_webview);
	    progressBar = (ProgressBar) findViewById(R.id.progress_bar);
	    
	    String actionUrl = getIntent().getStringExtra(Constants.PARM_ACTION_URL);
	    title = getIntent().getStringExtra(Constants.PARM_PAGE_TITLE);
	    
        advertise_webview.setHorizontalScrollBarEnabled(false);
        advertise_webview.setVerticalScrollBarEnabled(false);
        //填充webview
        advertise_webview.setNetworkAvailable(true);
        advertise_webview.getSettings().setJavaScriptEnabled(true);
        advertise_webview.getSettings().setDefaultTextEncodingName("utf-8");
        
        
        advertise_webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(final WebView view, String url) {
            	
            	if (url!=null&&(url.contains("questionnaire/beginQuestionnaireView")||url.contains("doctor/search")||url.contains("questionnaire/completeQuestionnaireView"))) {
            		initBackView(R.drawable.close_ico, 0, new OnClickListener() {
            			@Override
            			public void onClick(View v) {
            				   finish();
            			}
            		});
				}else {
					initBackView(R.drawable.left_back_white_icon, 0, new OnClickListener() {
            			@Override
            			public void onClick(View v) {
            				if (advertise_webview.canGoBack()) {
            					advertise_webview.goBack();
            					progressBar.setVisibility(View.VISIBLE);
            				}else {
            					finish();
            				}
            				
            			}
            		});
				}
            	
            	

            	 progressBar.setVisibility(View.VISIBLE);
                view.loadUrl(url+"?os=android");
                return true;
            }
            
            
            @Override
            public void onPageStarted(final WebView view, String url, Bitmap favicon) {
            	if (url!=null&&(url.contains("questionnaire/beginQuestionnaireView")||url.contains("doctor/search")||url.contains("questionnaire/completeQuestionnaireView"))) {
            		initBackView(R.drawable.close_ico, 0, new OnClickListener() {
            			@Override
            			public void onClick(View v) {
            				finish();
            			}
            		});
				}
            	
            	super.onPageStarted(view, url, favicon);
            }
            
            
            
            @Override
            public void onPageFinished(WebView view, String url) {

//            String title=view.getTitle();
//
//            if(title==null){
//
//            title="";
//
//            }
//
//            setTitleViewValue(title);

            super.onPageFinished(view, url);

            }
            
        });
        
        
        
        
        advertise_webview.setWebChromeClient(new WebChromeClient() {
        	
        	@Override  
            public void onReceivedTitle(WebView view, String title) {  
                
       		    super.onReceivedTitle(view, title);  
                setTitleViewValue(title);
            } 
        	
            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress >= 100) {
                    progressBar.setVisibility(View.GONE);
                }
            }
            
            
            
            //扩展浏览器上传文件
            //3.0++版本
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                openFileChooserImpl(uploadMsg);
            }

            //3.0--版本
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                openFileChooserImpl(uploadMsg);
            }

            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                openFileChooserImpl(uploadMsg);
            }

            // For Android > 5.0
            public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
                openFileChooserImplForAndroid5(uploadMsg);
                return true;
            }
        });
        
        
        
        
//        advertise_webview.setWebChromeClient(new WebChromeClient() {
//        	  //扩展浏览器上传文件
//            //3.0++版本
//            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
//                openFileChooserImpl(uploadMsg);
//            }
//
//            //3.0--版本
//            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
//                openFileChooserImpl(uploadMsg);
//            }
//
//            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
//                openFileChooserImpl(uploadMsg);
//            }
//
//            // For Android > 5.0
//            public boolean onShowFileChooser (WebView webView, ValueCallback<Uri[]> uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) {
//                openFileChooserImplForAndroid5(uploadMsg);
//                return true;
//            }
//          });
        
        
        
        advertise_webview.loadUrl(actionUrl);
    }
    
    
    
    public ValueCallback<Uri> mUploadMessage;
    public ValueCallback<Uri[]> mUploadMessageForAndroid5;

    public final static int FILECHOOSER_RESULTCODE = 1;
    public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
    
    
    
    private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
        mUploadMessage = uploadMsg;
        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("image/*");
        startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
    }

    private void openFileChooserImplForAndroid5(ValueCallback<Uri[]> uploadMsg) {
        mUploadMessageForAndroid5 = uploadMsg;
        Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
        contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
        contentSelectionIntent.setType("image/*");

        Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
        chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
        chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");

        startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == mUploadMessage)
                return;
            Uri result = intent == null || resultCode != RESULT_OK ? null: intent.getData();
            mUploadMessage.onReceiveValue(result);
            mUploadMessage = null;

        } else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_5){
            if (null == mUploadMessageForAndroid5)
                return;
            Uri result = (intent == null || resultCode != RESULT_OK) ? null: intent.getData();
            if (result != null) {
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
            } else {
                mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
            }
            mUploadMessageForAndroid5 = null;
        }
    }
    
    
    
    
    
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
//            //获取历史列表
//            WebBackForwardList mWebBackForwardList = webView
//                    .copyBackForwardList();
//            //判断当前历史列表是否最顶端,其实canGoBack已经判断过
//            if (mWebBackForwardList.getCurrentIndex() > 0) {
//                webView.goBack();
//                return true;
//            }
//        }
//        return super.onKeyDown(keyCode, event);
//    }
    
//    @Override
    // 设置回退
    // 覆盖Activity类的onKeyDown(int keyCoder,KeyEvent event)方法
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if ((keyCode == KeyEvent.KEYCODE_BACK) && advertise_webview.canGoBack()) {
//        	advertise_webview.goBack(); // goBack()表示返回WebView的上一页面
//            return true;
//        }
//        return super.onKeyDown(keyCode,event);
//    }
	
	@Override
	public void initHeaderView() {
		initBackView(R.drawable.close_ico, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (advertise_webview.canGoBack()) {
					advertise_webview.goBack();
					progressBar.setVisibility(View.VISIBLE);
				}else {
					finish();
				}
				
			}
		});
		setTitleViewValue(0, 0, R.color.white);
		setTitleViewValue(title);
	}
	
	
   

}
